package org.example.test.controller;

import org.example.test.dal.entity.User;
import org.example.test.dal.mapper.UserMapper;
import org.example.test.request.UserRequest;
import org.example.test.response.CommonResponse;
import org.example.test.response.ResponseBuilder;
import org.example.test.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理控制器
 * 提供用户注册、登录、地址管理等接口
 */
@RestController
@RequestMapping("/user")
public class UserController {

    // 日志记录器
    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;  // 用户服务层接口

    @Autowired
    private UserMapper userMapper;    // 用户数据访问接口

    /**
     * 用户注册接口
     * @param userRequest 注册请求参数，包含用户名、邮箱、密码等信息
     * @return 通用响应结果
     */
    @PostMapping("/register")
    public CommonResponse register(@RequestBody UserRequest userRequest) {
        try {
            LOGGER.info("register request: {}", userRequest.toString());
            // 调用服务层完成用户注册
            userService.register(userRequest);
            // 返回成功响应
            return ResponseBuilder.success();
        } catch (Exception e) {
            LOGGER.error("Error registering user: {}", e.getMessage());
            // 返回错误响应
            return ResponseBuilder.error(e.getMessage());
        }
    }

    /**
     * 用户登录接口
     * @param userRequest 登录请求参数，包含邮箱和密码
     * @return 通用响应结果，成功时返回邮箱信息
     */
    @PostMapping("/login")
    public CommonResponse login(@RequestBody UserRequest userRequest) {
        try {
            LOGGER.info("login request: email={}", userRequest.getEmail());
            String email = userRequest.getEmail();
            String password = String.valueOf(userRequest.getPassword());
            // 调用服务层完成用户登录验证
            userService.login(email, password);
            // 返回成功响应及邮箱信息
            return ResponseBuilder.success(email);
        } catch (Exception e) {
            LOGGER.error("Error logging in user: {}", e.getMessage());
            return ResponseBuilder.error(e.getMessage());
        }
    }

    /**
     * 修改用户地址接口
     * @param userName 用户名ID
     * @param address 新地址信息
     * @return 通用响应结果
     */
    @PostMapping("/editAddress")
    public CommonResponse editAddress(@RequestParam Long userName, @RequestParam String address) {
        try {
            LOGGER.info("editAddress request: userName={}", userName);
            // 调用服务层修改用户地址
            userService.editAddress(userName, address);
            return ResponseBuilder.success();
        } catch (Exception e) {
            LOGGER.error("Error editAddress in user: {}", e.getMessage());
            return ResponseBuilder.error(e.getMessage());
        }
    }

    /**
     * 获取用户地址接口
     * @param userId 用户ID
     * @return 通用响应结果，成功时返回地址信息
     */
    @GetMapping("/getAddress")
    public CommonResponse getAddress(@RequestParam("userId") Long userId) {
        try {
            LOGGER.info("getAddress request: userId={}", userId);
            // 通过Mapper直接查询用户信息
            User user = userMapper.selectByUserName(userId);
            String address = user.getAddress();
            // 返回地址信息
            return ResponseBuilder.success(address);
        } catch (Exception e) {
            LOGGER.error("Error getting address: {}", e.getMessage());
            return ResponseBuilder.error(e.getMessage());
        }
    }
}